<%@page import="com.egame.manage.biz.utils.ConstVar"%>
<%@page import="java.io.Console"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@page import="cn.egame.ext.EnumTypeExt.OperationActivityRecordType"%>
<%@page import="cn.egame.ext.EnumTypeExt.OperationActivityPosition"%>
<%@page import="cn.egame.interfaces.fl.FileUsedType"%>  
<%@page import="cn.egame.ext.op.OperationActivityInfo"%> 
<%@page import="cn.egame.common.util.Utils"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<% 
	int para = 0;
	if(request.getAttribute("para")!=null){
		para = (Integer)request.getAttribute("para");
	}
	String urlString = request.getContextPath() + "/tv/activity/set/HX.do?para=" + para; 
	
	OperationActivityInfo bean = new OperationActivityInfo();
	if(request.getAttribute("bean")!=null){
		bean = (OperationActivityInfo)request.getAttribute("bean");			
	}
	String filepath = "";
	if(request.getAttribute("filepath")!=null){
		filepath = (String)request.getAttribute("filepath");
	}
	
%>
<link type="text/css" rel="stylesheet" href="<%=request.getContextPath()%>/css/main.css"  />
<script type="text/javascript" src="<%=request.getContextPath()%>/js/jquery-1.4.2.min.js"></script>
<script type="text/javascript" src="<%=request.getContextPath()%>/js/ajaxfileupload.js"></script>
<script type="text/javascript" src="<%=request.getContextPath()%>/js/My97DatePicker/WdatePicker.js"></script>
<script type="text/javascript">
	$(document).ready(function() {
		$("#adPosi").bind("click",function(){			
			if($("#adPosi").val()==5){
				$("#imgSpan").hide();
			}			
		});
	});
	function savePic(id,name,width,height,type){
		//检查图片
		var filepath = document.getElementById(id).value;
		if(filepath==""){
		    alert("请选择上传的图片！");
		    return;
		}else {
			var extname = filepath.substring(filepath.lastIndexOf(".")+1,filepath.length);
			extname = extname.toLowerCase();
			if(extname!= "jpg"&&extname!= "jpeg"&&extname!= "gif"&&extname!= "png"){
	            alert("图片类型必须是.jpg,jpeg,gif,png中的一种");
	            return;
			}
		}
		var domain="<%=request.getContextPath() + "/tv/activity/image/width/height.do?name=" %>" + name +"&width=" + width + "&height=" + height + "&type=" + type;
		jQuery.ajaxFileUpload({
			url:domain,
			secureuri:false,
			fileElementId:id,
			dataType:"text",
			success: function (data) {		
				if(data.indexOf(">")>0){
					data = data.split(">")[1];
				}
				if(data.indexOf("<")>0){
					data = data.split("<")[0];
				}
				if(data.split(",")[0] == '-1'){
					alert("您上传的图片尺寸不符合规格!");
					return false;
				}else{
					document.getElementById("picId").value =  data.split(",")[0];
					document.getElementById("adimage").src = data.split(",")[1];
					return true;
				}
	          }
		});
		return true;
	}
	function getOptionValue(selectName){
		var select = document.getElementById(selectName);
		if(select == null)return null;
		var options = select.options;
		if(options == null)return null;
		var optionIndex = select.selectedIndex;
		if(optionIndex == null)return null;
		return options[optionIndex].value;
	}
	
	function changeLink(){
		var value = getOptionValue("recordTypeNum");
		if(value == <%=OperationActivityRecordType.advertisment.value() %>){
			document.getElementById("linklabel").innerHTML="游戏编号";
		}else{
			document.getElementById("linklabel").innerHTML="链接地址";
		}		
	}
	
	
	function formSubmit(){		
		var isSubmit = true;
		if($("#picId").val()==""){
			alert("请选择上传的图片！");
			isSubmit = false;
			return;
		}
		var td = $("#myform").find("td[name='notnone']");
		
		for(var i = 0; i<td.length; i++){
			if($(td[i]).find("input[type='text']").val() == "" || ($(td[i]).find("select")[0] && $(td[i]).find("option:selected").val() == 0)){
				$(td[i]).find("span").remove();
				$(td[i]).append("<span class='page_tip'>*必填</span>");
				isSubmit = false;
				return;
			}else{
				$(td[i]).find("span").remove();
			}
		}		
		if(isSubmit){
			document.getElementById("myform").submit();
		}else{
			return;
		}
	}
	function changeImageType(){		
		var imagetype = document.getElementById("imagetype").value;
		if(imagetype==1){
			document.getElementById("imgSpan").innerHTML = "图片尺寸 298*298, 	图片类型必须是gif,jpeg,jpg,png中的一种";
			document.getElementById("upload").href = "javascript:savePic('uploadFile','pic',298,298,<%=FileUsedType.ADVER_PHOTO %>)";
			document.getElementById("gId").value = imagetype;			
		}else if(imagetype==2){
			document.getElementById("imgSpan").innerHTML = "图片尺寸 601*601, 	图片类型必须是gif,jpeg,jpg,png中的一种";
			document.getElementById("upload").href = "javascript:savePic('uploadFile','pic',601,601,<%=FileUsedType.ADVER_PHOTO %>)";
			document.getElementById("gId").value = imagetype;
		}
		
		if(imagetype !=<%=bean.getgId() %>){
			alert("图片选择已更改，请上传符合要求的图片");
			document.getElementById("picId").value = "";
			document.getElementById("adimage").src = "";
		}else{
			document.getElementById("picId").value = "<%=bean.getPicId() %>";
			document.getElementById("adimage").src = "<%=filepath %>";
		}
	}
</script>
<title>新增页面</title>
</head>
<body>

<div class="matter_box">
	<div class="mt10 pt10 clearfix">
<form action=<%=urlString %> id="myform"  method="post" enctype="multipart/form-data">
	<input type="hidden" id="para" value="<%=para%>">
		<table class="table2">		
			<tr>
				<td>标题</td>
				<td name="notnone">
					<input type=hidden name=id value=<%=bean.getId() %> > 
					<input type=text name=description value=<%=bean.getDescription() %>>
				</td>
			</tr>
			<tr>
				<td>类型</td>
				<td name="notnone">
					<select id=recordTypeNum name=recordTypeNum onchange="changeLink()">
						<option value=<%=OperationActivityRecordType.none.value() %>>请选择</option>
						<option value=<%=OperationActivityRecordType.advertisment.value() %> <% if(bean.getRecordType() == OperationActivityRecordType.advertisment){ %>selected<% } %>>游戏</option>
						<option value=<%=OperationActivityRecordType.activity.value() %> <% if(bean.getRecordType() == OperationActivityRecordType.activity){ %>selected<% } %>>活动链接</option>
					</select>
				</td>
			</tr>
			<tr>
				<td>位置</td>
				<td name="notnone">
					<select name=position id="adPosi">
						<%-- <option value=<%=OperationActivityPosition.none.value() %>>请选择</option>
						<option value=<%=OperationActivityPosition.hotPosition.value() %> <% if(bean.getPosition() == OperationActivityPosition.hotPosition.value()){ %>selected<% } %>>热门</option>
						<option value=<%=OperationActivityPosition.newPosition.value() %> <% if(bean.getPosition() == OperationActivityPosition.newPosition.value()){ %>selected<% } %>>新品</option>
						<option value=<%=OperationActivityPosition.loadingPosition.value() %> <% if(bean.getPosition() == OperationActivityPosition.loadingPosition.value()){ %>selected<% } %>>加载页面</option>
						<option value=<%=OperationActivityPosition.popUpAds.value() %> <% if(bean.getPosition() == OperationActivityPosition.popUpAds.value()){ %>selected<% } %>>弹出广告</option> --%>
						<option value=<%=OperationActivityPosition.widgetPosition.value() %> selected>插件</option>
									
					</select>
				</td>
			</tr>
			<tr>
				<td>开始时间</td>
				<td name="notnone">
				<% 
					String time = "";
					if(bean.getBeginTime() != null){
						time = Utils.toDateString(bean.getBeginTime(),"yyyy-MM-dd hh:mm:ss");
					}					
				%>
					<input type=text name=beginString onclick="WdatePicker({dateFmt:'yyyy-MM-dd HH:mm:ss'})" class="Wdate" value="<%=time %>"></td>
			</tr>
			<tr>
				<td>结束时间</td>
				<td name="notnone">
				<% 
				String endtime = "";
				if(bean.getEndTime() != null){
					endtime = Utils.toDateString(bean.getEndTime(),"yyyy-MM-dd HH:mm:ss");
				}	
				%>
					<input type=text name=endString onclick="WdatePicker({dateFmt:'yyyy-MM-dd HH:mm:ss'})" class="Wdate" value="<%=endtime %>"></td>
			</tr>
			<tr>
				<td>图片选择</td>
				<td>
				<select id=imagetype name="imagetype" onchange="changeImageType()">
					<option value=1 <% if(bean.getgId()==1){ %>selected<%} %>>小图</option>
					<option value=2 <% if(bean.getgId()>1){ %>selected<%} %>>大图</option>
				</select>
				</td>
			</tr>
			<tr>
				<td>图片上传</td>
				<td name="notnone">
					<input type="hidden" name="file_type" id="file_type" value="<%=FileUsedType.ADVER_PHOTO %>">
					<input type=hidden name=picId id=picId value=<%=bean.getPicId() %>>
					<input type="file" name="pic" id="uploadFile">
					<a id="upload" href="javascript:savePic('uploadFile','pic',298,298,<%=FileUsedType.ADVER_PHOTO %>)"  class="btn2" >上传</a>
					<span style="color: #ff0000;margin-left: 15px;" id="imgSpan">		
					<% if(bean.getgId() == 1){ %>			
						图片尺寸 298*298, 图片类型必须是gif,jpeg,jpg,png中的一种
					<% }else if(bean.getgId() > 1){ %>
						图片尺寸 601*601, 图片类型必须是gif,jpeg,jpg,png中的一种
					<% } %>
					</span>
					<p>
					<img  id="adimage"  src="<%=filepath %>" width="200" height="100" > 
				</td>
			</tr>
			<tr>
				<td>
					<label id=linklabel>链接地址</label>
					
					</td>
				<td name="notnone">
					<input type=text name=linkUrl value=<%=bean.getLinkUrl() %>>
					<input type=hidden name=gId id=gId value=<%=bean.getgId() %>>
				</td>
			</tr>
			
			<tr>
				<td colspan=2>
					<input type=button onclick="formSubmit()" class="btn3" value="提交">
				</td>
			</tr>	
		</table>
	</form>
</div>
</div>
</body>
</html>